package com.ssm.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.UUID;

import sun.misc.BASE64Encoder;

public class UUIDUtil {
    private static String digits(long val, int digits) {
        long hi = 1L << (digits * 4);
        return Number.toString(hi | (val & (hi - 1)), Number.MAX_RADIX).substring(1);
    }

    /**
     * 以62进制（字母加数字）生成19位UUID，最短的UUID
     * 
     * @return
     */
    public static String uuid() {
        UUID uuid = UUID.randomUUID();
        StringBuilder sb = new StringBuilder();
        sb.append(digits(uuid.getMostSignificantBits() >> 32, 8));
        sb.append(digits(uuid.getMostSignificantBits() >> 16, 4));
        sb.append(digits(uuid.getMostSignificantBits(), 4));
        sb.append(digits(uuid.getLeastSignificantBits() >> 48, 4));
        sb.append(digits(uuid.getLeastSignificantBits(), 12));
        return sb.toString();
    }
    
    
    /**利用MD5进行加密
     * @param str  待加密的字符串
     * @return  加密后的字符串
     * @throws NoSuchAlgorithmException  没有这种产生消息摘要的算法
     * @throws UnsupportedEncodingException  
     */
    public static String EncoderByMd5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException{
        //确定计算方法
        MessageDigest md5=MessageDigest.getInstance("MD5");
        BASE64Encoder base64en = new BASE64Encoder();
        //加密后的字符串
        return base64en.encode(md5.digest(str.getBytes("utf-8")));
//        return newstr;
    }
    
    
    public static String getOrderNo(){       
    	String orderNo = "" ;        
    	String trandNo = String.valueOf((Math.random() * 9 + 1) * 1000000);        
    	long sdf = System.currentTimeMillis();
    	orderNo = trandNo.toString().substring(0, 4);        
    	orderNo = orderNo + sdf;      
    	return orderNo ;    
    }
    
    public static void main(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException
    {
    	Date d1 = new Date();
    	for(int i = 0; i < 1000000; i ++)
    	{
    		uuid();
    	}
    	Date d2 = new Date();
    	for(int i = 0; i < 1000000; i ++)
    	{
    		System.out.println(	EncoderByMd5("{\"attributes\":[{\"name\":\"ANSTYPE\",\"title\":\"应答标志\",\"value\":\"\"},{\"name\":\"AUTHTYPE\",\"title\":\"认证方式\",\"value\":\"0\"},{\"name\":\"CWXX\",\"title\":\"错误信息\",\"value\":\"160.9.224.72;1;0;*;10855861;#;A\"},{\"name\":\"EXTEND\",\"title\":\"扩展字段\",\"value\":\"\"},{\"name\":\"GDMM\",\"title\":\"股东密码\",\"value\":\"隐私不能显示\"},{\"name\":\"GDXM\",\"title\":\"股东姓名\",\"value\":\"\"},{\"name\":\"GDZH\",\"title\":\"股东帐户\",\"value\":\"010010002058\"},{\"name\":\"HTBH\",\"title\":\"合同编号\",\"value\":\"\"},{\"name\":\"JJZH\",\"title\":\"基金帐户\",\"value\":\"\"},{\"name\":\"LOGID\",\"title\":\"日志ID\",\"value\":\"5adfd96d\"},{\"name\":\"MMLB\",\"title\":\"买卖类别\",\"value\":\"\"},{\"name\":\"ndata\",\"title\":\"UI自动生成的数据\",\"value\":\"ND=1.0deptid=hddinfo=clientver=E065.18.61cpuinfo=Intel(R) Xeon(R) CPU E5-2650 v4 @ 2.20GHz*4crypt_name=HEXINcomputername=K0560002dynsid=c_support=111111111111clientver2=ptgp_sptggt=0macinfo=00-50-56-b9-31-67;verinfo=htsc015.18.58hxui_ver=3.0publicip=hardwarecode=BIOS:1D0064395AF3C745F6C3194E92373D7A,HDD:Err:00000001,MAC:00-50-56-B9-31-67,PI:C,NTFS,59,VOLSERIAL:cc9f913c,VOLNAME:guid={068D82AD-98E6-4786-AA92-5E73DA1AB55A}menuid=0scdm=2menu_ver=20100705client=exejdmm=1serverip=168.60.61.61volume_lable=cc9f913clocalip=cpuid=000406F1-00000000-00000000volume_name=bios_date=09/21/2015lgtime=1524619554atype=0verified=6sumreq=20wtsuc=0wtfail=0cdsuc=0cdfail=0qucnt=3other=17cwxx=\"},{\"name\":\"PID\",\"title\":\"请求PID\",\"value\":\"7b72ab83\"},{\"name\":\"PRI_EXP\",\"title\":\"未命名\",\"value\":\"\"},{\"name\":\"REQTYPE\",\"title\":\"请求类别\",\"value\":\"S\"},{\"name\":\"SHZH\",\"title\":\"上海帐号\",\"value\":\"\"},{\"name\":\"SZZH\",\"title\":\"深圳帐号\",\"value\":\"\"},{\"name\":\"UID\",\"title\":\"唯一ID\",\"value\":\"a5a5b5\"},{\"name\":\"VERIFIED\",\"title\":\"退出时的认证状态\",\"value\":\"6\"},{\"name\":\"WTJG\",\"title\":\"委托价格\",\"value\":\"0.000\"},{\"name\":\"WTSL\",\"title\":\"委托数量\",\"value\":\"10855861\"},{\"name\":\"WTSVRNAME\",\"title\":\"委托主站名称\",\"value\":\"华泰证券（1）\"},{\"name\":\"ZJZH\",\"title\":\"资金帐户\",\"value\":\"\"},{\"name\":\"ZQDM\",\"title\":\"证券代码\",\"value\":\"\"}],\"date\":\"20180425\",\"fileName\":\"20180425-01.dat_解析后.log\",\"fileOrder\":\"1\",\"id\":\"mf6k1zQj8FMI7aTpmh5\",\"logType\":\"0\",\"response\":null,\"seq\":\"42\",\"time\":\"09:27:09.000\",\"title\":\"1001_客户退出\"}"+i));
    	}
    	Date d3 = new Date();
    	
//    	System.out.println(d2.getTime() - d1.getTime());
//    	
//    	System.out.println(d3.getTime() - d2.getTime());
    }
}
